﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Utility.BehaviourTree
{
	public abstract class DecoratorNode : BTNode
	{
		public BTNode ChildNode { get; protected set; }

		public BTNode Add (BTNode child)
		{
			base._AddChild (child);
			ChildNode = child;
			return this;
		}

		protected override bool OnPreUpdate ()
		{
			if (ChildNode == null)
			{
				Error ("DecoratorNode must have a child");
				return false;
			}
			return true;
		}

		protected override bool OnPostUpdate ()
		{
			if (ChildNode.IsError)
			{
				Error ("Child node error");
				return false;
			}
			if (!base.OnPostUpdate ())
			{
				return false;
			}
			return true;
		}

		protected override void OnReset ()
		{
			base.OnReset ();
			ChildNode.Reset ();
		}

		public override StringBuilder BuildString (StringBuilder stringbuilder = null, int depth = 0)
		{
			stringbuilder = base.BuildString (stringbuilder, depth);
			ChildNode.BuildString (stringbuilder, depth + 1);
			return stringbuilder;
		}
	}

}